package com.steven.ourcontest.chat.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.steven.ourcontest.base.BaseViewModel
import com.steven.ourcontest.chat.api.ChatApi
import com.steven.ourcontest.chat.bean.UserDetailRes
import com.steven.ourcontest.team.api.TeamApi
import com.steven.ourcontest.team.bean.TeamInfoRes
import com.steven.ourcontest.utils.plusAssign

class UserDetailViewModel : BaseViewModel() {
    private val _userDetailResponse = MutableLiveData<UserDetailRes>()
    val userDetailResponse: LiveData<UserDetailRes>
        get() = _userDetailResponse

    private val _userTeamResponse = MutableLiveData<TeamInfoRes>()
    val userTeamResponse: LiveData<TeamInfoRes>
        get() = _userTeamResponse

    fun queryUserDetail(uid: Long) {
        disposed += ChatApi.instance.queryUserDetail(uid)
            .subscribe({
                _userDetailResponse.value = it
            }, {
                it.printStackTrace()
            })
    }

    fun sendApplyToFriend(friendId: Long, success: (() -> Unit)) {
        disposed += ChatApi.instance.sendApplyToFriend(friendId)
            .subscribe({
                success.invoke()
            }, {
                it.printStackTrace()
            })
    }

    fun queryUserTeam(friendId: Long) {
        disposed += TeamApi.instance.queryUserTeam(friendId)
            .subscribe({
                _userTeamResponse.value = it
            }, {
                it.printStackTrace()
            })
    }

    fun applyToTeam(teamId: Long, success: () -> Unit, error: (msg:String) -> Unit) {
        disposed += TeamApi.instance.applyToTeam(teamId)
            .subscribe({
                success.invoke()
            }, {
                it.message?.let { it1 -> error.invoke(it1) }
                it.printStackTrace()
            })
    }

    fun addBan(userId: Long, success: () -> Unit, error: (msg:String) -> Unit){
        disposed += ChatApi.instance.addUserBan(userId)
            .subscribe({
                success.invoke()
            }, {
                it.message?.let { it1 -> error.invoke(it1) }
                it.printStackTrace()
            })
    }

    fun deleteBan(userId: Long, success: () -> Unit, error: (msg:String) -> Unit){
        disposed += ChatApi.instance.deleteUserBan(userId)
            .subscribe({
                success.invoke()
            }, {
                it.message?.let { it1 -> error.invoke(it1) }
                it.printStackTrace()
            })
    }
}